//
//  MineViewController.m
//  Tongcheng
//
//  Created by 邢广范 on 2017/4/3.
//  Copyright © 2017年 Tongcheng. All rights reserved.
//我的 个人中心界面

#import "MineViewController.h"
#import "MineHeaderView.h"
#import "MineTableViewCell.h"
#import "MineZuanViewController.h"
#import "MineYanzhengViewController.h"
#import "MineMoneyViewController.h"
#import "MineSettingViewController.h"
#import "Minemodel.h"
#import "MineFocusViewController.h"
#import "MineGiftViewController.h"
#import "MineLookViewController.h"
#import "MineBuyYBViewController.h"
#import "MineOperator.h"
#import "MineUserinfoModel.h"
#import "MineUserinfoDataModel.h"
#import "MineDataViewController.h"
#import "MinePhotoViewController.h"
#import "MineBuyYBViewController.h"


@interface MineViewController ()<UITableViewDelegate,UITableViewDataSource,MineHeaderViewDelegate>

@property (weak, nonatomic) IBOutlet UITableView *tableView;

@property (nonatomic, strong) NSArray *dataArray;

@property (nonatomic, strong) MineUserinfoDataModel *dataModel;
@property (nonatomic, strong)MineHeaderView *headerView;

@end

@implementation MineViewController

static NSString *mineCellIdentifier = @"MineTableViewCell";

- (void)setDataArray:(NSArray *)dataArray
{
    _dataArray = dataArray;
    [self.tableView reloadData];
}

- (void)setupNavigation
{
    self.navigationItem.title = @"个人中心";
}

- (void)setupView
{
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.tableFooterView = [[UIView alloc] init];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
}

- (void)setupHeaderView
{
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 171)];
    _headerView = [MineHeaderView gainMineHeaderView];
    _headerView.frame = view.bounds;
    _headerView.delegate = self;
    _headerView.model = self.dataModel;
    [view addSubview:_headerView];
    self.tableView.tableHeaderView = view;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupNavigation];
    [self setupView];
    [self setupHeaderView];
    // Do any additional setup after loading the view from its nib.
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    MineTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:mineCellIdentifier];
    if (!cell) {
        cell = [[UINib nibWithNibName:@"MineTableViewCell" bundle:nil] instantiateWithOwner:self options:nil].lastObject;
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    if (indexPath.section == 0) {
        if (indexPath.row == 0) {
            //抢夺记录
            Minemodel *model = self.dataArray[0];
            cell.model = model;
        }else{
            //谁关注我
            Minemodel *model = self.dataArray[1];
            cell.model = model;
        }
    }else if (indexPath.section == 1){
        if (indexPath.row == 0) {
            //我的钱包
            Minemodel *model = self.dataArray[2];
            cell.model = model;
        }else if (indexPath.row == 1){
            //我的Y币
            Minemodel *model = self.dataArray[3];
            cell.model = model;
        }else if (indexPath.row == 2){
            //我的钻石
            Minemodel *model = self.dataArray[4];
            cell.model = model;
        }else if (indexPath.row == 3){
            //我的礼物
            Minemodel *model = self.dataArray[5];
            cell.model = model;
        }else{
            //最近来访
            Minemodel *model = self.dataArray[6];
            cell.model = model;
        }
    }else if (indexPath.section == 2){
        if (indexPath.row == 0) {
            //个人资料
            Minemodel *model = self.dataArray[7];
            cell.model = model;
        }else{
            //我的相册
            Minemodel *model = self.dataArray[8];
            cell.model = model;
        }
    }else{
        //设置中心
        Minemodel *model = self.dataArray[9];
        cell.model = model;
    }
    return cell;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section == 0) {
        return 2;
    }else if (section == 1){
        return 5;
    }else if (section == 2){
        return 2;
    }else{
        return 1;
    }
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 4;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 50;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    if (section == 3) {
        return 0;
    }else{
       return 10;
    }
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 10)];
    view.backgroundColor = RGB(0xF5F5F5);
    return view;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        if (indexPath.row == 0) {
            //抢夺记录
        }else{
            //谁关注我
            MineFocusViewController *focusVc = [[MineFocusViewController alloc] init];
            [self.navigationController pushViewController:focusVc animated:YES];
        }
    }else if (indexPath.section == 1){
        if (indexPath.row == 0) {
            //我的钱包
            MineMoneyViewController *moneyVc = [[MineMoneyViewController alloc] init];
            moneyVc.moneyCount = self.dataModel.cashing;
            [self.navigationController pushViewController:moneyVc animated:YES];
        }else if (indexPath.row == 1){
            //我的Y币
            MineBuyYBViewController *yb = [[MineBuyYBViewController alloc] init];
            yb.index = 0;
            [self.navigationController pushViewController:yb animated:YES];
        }else if (indexPath.row == 2){
            //我的钻石
            MineZuanViewController *zuanVc = [[MineZuanViewController alloc] init];
            [self.navigationController pushViewController:zuanVc animated:YES];
        }else if (indexPath.row == 3){
            //我的礼物
            MineGiftViewController *giftVc = [[MineGiftViewController alloc] init];
            [self.navigationController pushViewController:giftVc animated:YES];
        }else{
            //最近来访
            MineLookViewController *lookVc = [[MineLookViewController alloc] init];
            [self.navigationController pushViewController:lookVc animated:YES];
        }
    }else if (indexPath.section == 2){
        if (indexPath.row == 0) {
            MineDataViewController *data = [[MineDataViewController alloc] init];
            data.model = self.dataModel;
            [self.navigationController pushViewController:data animated:YES];
            //个人资料
        }else{
            //我的相册
            MinePhotoViewController *photo = [[MinePhotoViewController alloc] init];
            [self.navigationController pushViewController:photo animated:NO];
        }
    }else{
        //设置中心
        MineSettingViewController *setVc = [[MineSettingViewController alloc] init];
        [self.navigationController pushViewController:setVc animated:YES];
    }
}

#pragma mark -- MineHeaderViewDelegate
- (void)clickAvatarImageViewWith:(UIImageView *)imageView
{
//   if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]) {
//            UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init];
//            imagePickerController.delegate = self;
//            imagePickerController.allowsEditing = YES;
//            imagePickerController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
//            
//            [self presentViewController:imagePickerController animated:YES completion:^{
//                
//            }];
//        }
//        else
//        {
//            NSLog(@"手机不支持相册");
//        }
//    
}

//- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info{
//    [picker dismissViewControllerAnimated:YES completion:NULL];
//    UIImage *image = [info objectForKey:UIImagePickerControllerEditedImage];
//    _headerView.avatarImageView.image = image;
//    
//}

- (void)clickBuyVipBtn
{
    MineBuyYBViewController *yb = [[MineBuyYBViewController alloc] init];
    yb.index = 1;
    [self.navigationController pushViewController:yb animated:YES];
}

- (void)clickYanzhengBtn
{
    MineYanzhengViewController *vc = [[MineYanzhengViewController alloc] init];
    [self.navigationController pushViewController:vc animated:YES];
}

- (void)clickCopyBtn
{
    
}

- (void)download
{
    NSMutableDictionary *dic = [NSMutableDictionary dictionary];
    dic[@"token"] = self.config.token;
    dic[@"user_id"] = self.config.userId;
    MineOperator *operator = [[MineOperator alloc] initWithParams:dic];
    [self.networkingManager asyncTaskOnVC:self operator:operator successBlock:^(BaseModel *baseModel) {
        if ([baseModel.code isEqualToString:@"1"]) {
            self.dataModel = operator.data.data;
            //重组数组
            [self setupDataWith:self.dataModel];
            [self setupHeaderView];
        }
    } failBlock:^(BaseModel *baseModel) {
        
    }];
}

- (void)setupDataWith:(MineUserinfoDataModel *)infoModel
{
    NSMutableArray *array = [NSMutableArray array];
    Minemodel *model1 = [[Minemodel alloc] initWithImg:@"抢夺记录" withTitle:@"抢夺记录" withCount:nil];
    [array addObject:model1];
    Minemodel *model2 = [[Minemodel alloc] initWithImg:@"眼睛" withTitle:@"谁关注我" withCount:nil];
    [array addObject:model2];
    Minemodel *model3 = [[Minemodel alloc] initWithImg:@"我的钱包" withTitle:@"我的钱包" withCount:infoModel.redpack];
    [array addObject:model3];
    Minemodel *model4 = [[Minemodel alloc] initWithImg:@"我的Y币" withTitle:@"我的Y币" withCount:infoModel.money];
    [array addObject:model4];
    Minemodel *model5 = [[Minemodel alloc] initWithImg:@"我的钻石" withTitle:@"我的钻石" withCount:@"0"];
    [array addObject:model5];
    Minemodel *model6 = [[Minemodel alloc] initWithImg:@"我的礼物" withTitle:@"我的礼物" withCount:nil];
    [array addObject:model6];
    
    Minemodel *model7 = [[Minemodel alloc] initWithImg:@"最近来访" withTitle:@"最近来访" withCount:nil];
    [array addObject:model7];
    
    Minemodel *model8 = [[Minemodel alloc] initWithImg:@"个人资料" withTitle:@"个人资料" withCount:nil];
    [array addObject:model8];
    
    Minemodel *model9 = [[Minemodel alloc] initWithImg:@"我的相册" withTitle:@"我的相册" withCount:nil];
    [array addObject:model9];
    
    Minemodel *model10 = [[Minemodel alloc] initWithImg:@"设置中心" withTitle:@"设置中心" withCount:nil];
    [array addObject:model10];
    
    self.dataArray = array;
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self download];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
